<?php

if (!defined('PATH'))
	exit();

class Online extends Controller {

	protected $default_action = '';
	public $_title = 'Who is online?';

	public function __construct() {
		parent::__construct();
	}

	public function action_() {
		$options = array(
			'name' => 'desc',
			'level' => 'desc',
			'vocation' => 'desc'
		);
		$sort = array();

		if (isset($this->paramsURI[1]) && isset($this->paramsURI[2]) && in_array($this->paramsURI[1], array('level', 'name', 'vocation')) && in_array($this->paramsURI[2], array('asc', 'desc'))) {
			$sort[0] = $this->paramsURI[1];
			$sort[1] = $this->paramsURI[2];
			if ($options[$sort[0]] == 'desc' && $sort[1] == 'desc')
				$options[$sort[0]] = 'asc';
			if ($sort[0] == 'vocation')
				$sort[0] = 'promotion ' . $sort[1] . ', vocation';
		} else {
			$sort[0] = 'name';
			$sort[1] = 'asc';
		}
		$view = new View('online/index');
		$query = $this->db->query('SELECT `name`, `level`, `vocation`, `promotion` FROM `players` WHERE `online` = 1 ORDER BY ' . $sort[0] . ' ' . $sort[1]);
		$view->query = $query;
		$view->sort = $options;
		$view->render();
	}
}

?>